package com.lishem.rpc.client.cluster.impl;

import com.lishem.common.utils.IpUtil;
import com.lishem.rpc.client.channel.ProviderService;
import com.lishem.rpc.client.cluster.ClusterStrategy;

import java.util.List;

/**
 * HASH策略
 */
public class HashClusterStrategyImpl implements ClusterStrategy {

    @Override
    public ProviderService select(List<ProviderService> serviceRoutes) {
        // 根据IP获取HASH值，选取对应的服务
        String realIp = IpUtil.getRealIp();
        int hashCode = realIp.hashCode();
        int size = serviceRoutes.size();
        return serviceRoutes.get(hashCode % size);
    }
}
